import React,{Component} from 'react'
import { message,Tooltip } from 'antd';
import {Link} from 'react-router'
import '../style/less/footer.less';
import * as fetch from '../config/fetch';
import * as pub from '../config/public';
class Login extends Component {
	constructor(args) {
		super();
		this.state = {
			username: '',
			password: '',
			isRemember: true,
			coded_password:'',
			error_username:false,
			error_password:false
		}
	}
	componentWillMount() {
		if (localStorage.getItem('isRemember') === 'true') {
			this.setState({
				isRemember: localStorage.getItem('isRemember'),
				username: localStorage.getItem('username'),
				password: localStorage.getItem('password')
			}, () => {
				// this.refs.password.type = 'password'
			})
		} else {

		}
	}
	login = (e) => {
		e.preventDefault();
		// if (!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(this.state.password))) {
		// 	message.error('请输入正确的手机号', 1);
		// 	return;
		// }
		if (this.state.username === '') {
			this.setState({
				error_username: true
			})
			return;
		} else {
			this.setState({
				error_username: false
			})
		}
		if (this.state.password === '') {
			this.setState({
				error_password: true
			})
			return;
		} else {
			this.setState({
				error_password: false
			})
		}
		let user = {
			name: this.state.username,
			password: this.state.password
		};
		// console.log(455666,this.state.username)
		// if(this.state.username.value.replace( /^[0-9]{11}$/,'')==){
		// 	message.error()
		// }
		fetch.login(user.name, user.password).then((data) => {
			if (data.result === 'RC100') {

				let info = data.menu;

				let menu = {};
				menu.admin_menu = [];
				menu.learn_menu = [];
				menu.personal_menu = [];
				let menu_data = [];
				let job = [];

				for (let user of info) {
					job.push(user.job);
					if (user.job === '学员') {
						menu_data = user.nodeResult;
					}
					if (user.job === '管理员') {
						menu.admin_menu = user.nodeResult;
					}
				}

				for (let x of menu_data) {
					if (x.parentNodeCode === '0') {
						menu.learn_menu.push(x);
					} else {
						menu.personal_menu.push(x);
					}
				}
				let user = {};
				user.ldToken = data.ldToken;
				user.userType = data.userType;
				user.userCode = data.userCode;
				user.job = job;
				user.menu = menu;
				user.username = this.state.username;
				user.manageCom = data.manageCom;
				user.deadTime = data.deadTime;
				user.channelId = data.channelId;


				localStorage.setItem("isRemember", this.state.isRemember);
				if (this.state.isRemember) {
					localStorage.setItem("username", this.state.username);
					localStorage.setItem("password", this.state.password);
				}
				localStorage.setItem("ldToken", data.ldToken);
				localStorage.removeItem('user');
				localStorage.setItem("user", JSON.stringify(user));
				pub.save_user();

				let userType = data.userType;

				if (userType === '学员') {
					localStorage.setItem("now_userType", userType);
					pub.jumpHistory.push('/');
					return;
				}
				
				pub.jumpHistory.push('/Entrance');

			} else {
				message.error(data.errMsg, 3);
			}
		}, (res) => {
			pub.reject(res);
		})

	}
	changePassword(e) {
		this.setState({
			password: e.target.value.replace(/^ +| +$/g, '')
		}, () => {
			// this.refs.password.type = 'password'
		})
	}
	InputNumber(e) {
		this.setState({
			username: e.target.value
		})

		// if(e.target.value.length>11){
		// 	message.error("只能为11位数");	

		// }else{
		// 	this.setState({
		// 		username:e.target.value.replace(/[^\d]/g,'')
		// 	})
		// }

	}
	render(){
		return(    
		<div className="login-bg">
			<div className="login-box">
				<div className="login-logo"><div className="logo"></div><img alt='test' className="login-logoImg" src={require('../style/images/font-style@2x.png')} width="200" style={{verticalAlign: 'middle'}}/></div>
				<form className="login-input" onSubmit={(e)=>this.login(e)}>
					<label>
					<Tooltip visible={this.state.error_username} title="请输入手机号/工号/邮箱">
					<input value={this.state.username} onChange={(e)=>this.InputNumber(e)} type="text" placeholder="请输入手机号/工号/邮箱" />
					</Tooltip>
					</label>
					<label>
					<Tooltip visible={this.state.error_password} title="请输入密码">
					<input ref='password' value={this.state.password} onChange={(e)=>this.changePassword(e)} type="password" placeholder="密码"/>
					</Tooltip>
					</label>
{/*					<label style={{display:'none'}}><input type="text" placeholder="隐藏"/></label>
					<label style={{display:'none'}}><input type="password" placeholder="隐藏"/></label>*/}
					<div className="row rember">
						<label><input type="checkbox" checked={this.state.isRemember} onChange={()=>{this.setState({isRemember:!this.state.isRemember})}}/>记住密码</label>
						<Link to='BackPassword'>找回密码</Link>
					</div>
					<button type='submit' className="login-btn">登&emsp;录</button>
					<a href="http://down-www.newasp.net/soft/soft1/chrome_xp_installer.rar" download="http://down-www.newasp.net/soft/soft1/chrome_xp_installer.rar" style={{display: 'block',margin: '40px 0 20px',color: '#6EACF7'}}>下载插件</a>
				    <div style={{width: '400px',color: 'rgba(74,74,74,.5)'}}>支持浏览器版本: IE10以上、Chrome、Firefox<br/>                                         
特别说明: 视频业务流程仅仅支持Chrome版本或使用平台提供的ChRome绿色客户端</div>
				</form>
			</div>
		</div>
			)
	}
}
export default Login;